from tkinter import *
import time
import math
from mymodule.game_02_base import GameBase
from mymodule.game_02_base import Environment
from mymodule.game_02_base import Ball
from mymodule.game_02_base import Border
    
class BallGame(GameBase):
    def __init__(self):
        super().__init__()
    
    def check_hit_wall(self, ball, border):
        if(ball.x+ball.velocity_x<=border.left):
            ball.velocity_x = abs( ball.velocity_x)
                
        if(ball.x+ball.velocity_x+ball.radius>=border.right):
            ball.velocity_x = - abs( ball.velocity_x)
                
        if(ball.y+ball.velocity_y<=border.top):
            ball.velocity_y = abs( ball.velocity_y)

        if(ball.y+ball.velocity_y+ball.radius >=border.bottom):  
            ball.velocity_y = - abs( ball.velocity_y)
        
        
    def run(self):
        border = Border(100,700,100,500)
        self.make_wall(border.left,border.top,border.right-border.left,border.bottom-border.top)
        
        
        # 准备一个有多个球的“列表”
        balls = [
                self.make_ball(100, 150, 20, 2,2, "darkblue"),
                self.make_ball(200, 250, 25, -4,-4, "orange"),
                self.make_ball(300, 350, 10, -2,-2, "green"),
                self.make_ball(400, 450, 5, 4,4, "darkgreen")
                ]
        
       
        while True:
             for ball in balls:
                self.move_ball(ball)
                self.check_hit_wall(ball, border)
                self.redraw_ball(ball)
                self.tk.update()
                time.sleep(Environment.DURATION)
            
game = BallGame()
game.run()